(function(angular) {
	"use strict";
	angular.module("fs.waf").controller("cls.custPay.viewController", viewController);
	viewController.$inject = [ "$scope", "$q", "$filter", "modalService", "gridService", "layoutConfig", "validatorConfig", "authService", "custPayService", "settlementPaymentService",
			"settlementSummaryService", "glEntryService" ];
	function viewController($scope, $q, $filter, modalService, gridService, layoutConfig, validatorConfig, authService, custPayService, settlementPaymentService, settlementSummaryService,
			glEntryService) {
		var ctrl = this, custPayInfo = $scope.$stateParams.params, settlement = {};
		ctrl.validatorConfig = validatorConfig;
		ctrl.setlDetailsConfig = layoutConfig.formConfigs.setlDetailView.tableConfigs.setlDetails.columnDefs;
		ctrl.isDetailsPay = function(item) {
			return settlementSummaryService.isDetailsPay(item);
		};

		ctrl.isDetailsReceive = function(item) {
			return settlementSummaryService.isDetailsReceive(item);
		};
		ctrl.init = function() {
			ctrl.txnSetlInsts = gridService.createInnerGridOptions($scope, layoutConfig.formConfigs.txnSetlInsts.tableConfigs.txnSetlInsts, null);
			ctrl.txnSetlInsts.data = "vm.settlement.txnSetlInsts";
			ctrl.txnSetlInsts.enableRowHeaderSelection = false;
			var params = {
				custpayId : custPayInfo.custpayId
			};
			var defer1 = $q.defer(), defer2 = $q.defer(), defer3 = $q.defer();
			settlementSummaryService.getFxRateTypes().then(function(fxRateTypes) {
				settlement.fxRateTypes = fxRateTypes;
				defer1.resolve();
			});
			settlementPaymentService.settlementPaymentOptions().then(function(map) {
				settlement.deliveryTypeOptions = map.deliveryTypeOptions;
				settlement.currencyOptions = map.currencyOptions;
				settlement.allPartyTypeOptions = map.partyTypeOptions;
				settlement.swiftCaseList = map.swiftCaseList;
				defer2.resolve();
			});
			custPayService.initLogDataOptions().then(function(data) {
				ctrl.payCcyOptions = data.currency;
				ctrl.countryOptions = data.country;
				ctrl.prodTypeOptions = data.prodType;
				defer3.resolve();
			});
			($q.all({
				defer1 : defer1.promise,
				defer2 : defer2.promise,
				defer3 : defer3.promise
			})).then(function() {
				return $q(function(resolved, rejected) {
					authService.view(params).then(function(response) {
						settlement = angular.merge(settlement, response.data);
						resolved(settlement);
					});
				});
			}).then(function(settlement){
				angular.extend(ctrl, settlementPaymentService.getPayDetailsConfig(settlement, layoutConfig));
				settlementSummaryService.initData(settlement);
				ctrl.payPartyConfig = settlementPaymentService.initData(settlement, layoutConfig);
				processTxnSetlInsts(settlement);
				getGlEntryList(ctrl, settlement);
				custPayService.getLayout("custPay", "view", settlement.custpay.prodType, true).then(function(response) {
					ctrl.settlement = settlement;
					ctrl.layoutConfig = response || layoutConfig;
					ctrl.custPay = settlement.custpay;
				});
			});
		};

		function getGlEntryList(ctrl, settlement) {
			glEntryService.getGlentryList($scope, layoutConfig, settlement.settlementInfo.parentId, "CLS:" + settlement.settlementInfo.parentType).then(function(response) {
				ctrl.glEntryListConfig = response.glEntryListConfig;
				ctrl.glEntryList = response.glEntryList;
			});
		}

		function processTxnSetlInsts(settlement) {
			if (settlement.txnSetlInsts) {
				angular.forEach(settlement.txnSetlInsts, function(data, index, array) {
					data.paycodeName = data.paycode ? data.paycode.substr(7) : "";
					data.itemType_desc = $filter("translate")("label.settlement.init." + data.itemType.toLowerCase());
				});
			}
		}

		ctrl.viewGlentry = function() {
			modalService.openCustomDialogs({
				"title" : "popup.loan.glentrylist",
				"dialogsModule" : "cls.loan.glEntryList",
				"size" : [ 900, 400 ],
				"params" : {
					"refTxnId" : $filter("encode")(ctrl.custPay.custpayId),
					"refTxnType" : "CLS:CUSTPAY"
				},
				"btnCallback" : {
					"ok" : function() {
					}
				}
			});
		};
		
		ctrl.viewFee = function(){
			modalService.openCustomDialogs({
			   "title": "popup.title.txnfeelist",
			   "dialogsModule" : "cls.loan.txnFeeList",
			   "size" : [1300, 300],
			   "params": {
				   "txnId" : $filter("encode")(ctrl.custPay.custpayId)
			   },
			   "btnCallback" : {
					"ok" : function() {
					}
				}	
			});
		};

		// settlement payment
		ctrl.swiftTypeCallBack = function(result) {
			settlementPaymentService.swiftTypeCallBack(ctrl.settlement, layoutConfig, result);
		};

		ctrl.partyCountryChanged = function(payParty) {
			settlementPaymentService.partyCountryChanged(ctrl.settlement, payParty);
		};

		ctrl.partyStateChanged = function(payParty) {
			settlementPaymentService.partyStateChanged(ctrl.settlement, payParty);
		};
	}
})(angular);
